package com.one.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.one.bean.Cart;
import com.one.bean.CollectFootpoint;
import com.one.mapper.CartMapper;
import com.one.mapper.CollectFootPointMapper;
import com.one.service.CartService;
import com.one.vo.ProductParamVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Create on 2022-01-20
 * <p>
 * Author: LiangWei
 * <p>
 * Description: 购物车接口实现类
 **/
@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private CollectFootPointMapper collectFootPointMapper;
    @Override
    public int addProductToCart(Integer userId,ProductParamVo productParamVo) {
        QueryWrapper<Cart> wrapper = new QueryWrapper<>();
        wrapper.eq("product_id",productParamVo.getProdId());
        wrapper.eq("user_id",userId);
        wrapper.eq("tag_colors",productParamVo.getColorId());
        wrapper.eq("tag_size",productParamVo.getSizeId());
        Cart cart1 = cartMapper.selectOne(wrapper);
        if(cart1!=null){
            Integer producnum = cart1.getProduc();
            cart1.setProduc(productParamVo.getProductNum()+producnum);
            cart1.setUpdateDate(new Date());
            return cartMapper.updateById(cart1);
        }
        Cart cart = new Cart();
        cart.setProductId(productParamVo.getProdId());//该用户购物车插入该商品id
        cart.setUserId(userId);//插入用户id
        cart.setTagColors(productParamVo.getColorId());//该用户购物车插入该商品颜色
        cart.setTagSize(productParamVo.getSizeId());//该用户购物车插入该商品尺寸
        cart.setProduc(productParamVo.getProductNum());//该用户购物车插入该商品数量
        cart.setCreateDate(new Date());//创建时间
        cart.setUpdateDate(new Date());//更新时间
        return cartMapper.insert(cart);
    }

    @Override
    public int deleteProductFromCart(Integer userId,Integer cartId) {
//        return cartMapper.deleteById(cartId);
        return cartMapper.delete(new QueryWrapper<Cart>().eq("id",cartId).eq("user_id",userId));
    }

    @Override
    public int deleteManyProductsFromCart(List<Integer> cartIdList) {
        return cartMapper.deleteBatchIds(cartIdList);
    }

    @Override
    public List<Cart> selectAllCartsByUserId(Integer userId) {
        QueryWrapper<Cart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        return cartMapper.selectList(wrapper);
    }

    @Override
    public int deleteAllCartsByUserId(Integer userId) {
        QueryWrapper<Cart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        return cartMapper.delete(wrapper);
    }

    @Override
    public int updateProductNumByCartId(Integer productNum,Integer cartId) {
        Cart cart = new Cart();
        cart.setId(cartId);
        cart.setProduc(productNum);
        return cartMapper.updateById(cart);
    }

    @Override
    public int moveToCollectByUserIdAndProductId(Integer userId, Integer productId) {
        Map<String,Object> map = new HashMap<>();
        map.put("user_id",userId);
        map.put("product_id",productId);
        if(collectFootPointMapper.selectByMap(map)==null){
            CollectFootpoint collectFootpoint = new CollectFootpoint();
            collectFootpoint.setUserId(userId);
            collectFootpoint.setProductId(productId);
            collectFootpoint.setType(0);
            collectFootpoint.setCreateDate(new Date());
            collectFootpoint.setUpdateDate(new Date());
            collectFootpoint.setStatus(1);
            return collectFootPointMapper.insert(collectFootpoint);
        }
        return -1;
    }

    @Override
    public Cart selectOneCartByCartId(Integer cartId) {
        return cartMapper.selectById(cartId);
    }
}
